from urllib.request import urlopen
from io import BytesIO
import re
import gzip

class pokeparser:
	def __init__(self):
		self.mainurl = 'https://wiki.52poke.com'
		self.maintext = self.gettext(self.mainurl)
	# 获得网页源代码：
	def gettext(self, url):
		html = urlopen(url).read()
		try:
			buff = BytesIO(html)
			f = gzip.GzipFile(fileobj = buff)
			text = f.read().decode('utf-8')
		except:
			text = html.decode('utf-8')
		return text
	# 转换为简体中文网址：
	def combine(self, url):
		return self.mainurl+url.replace('wiki', 'zh-hans')
	# 将txt文件中每一行记录转换为字符串列表：
	def convert(self, line):
		line = line.strip('[]\n')
		fields = [f.strip(" '") for f in line.split(', ')]
		return fields

	# 获得图鉴列表网址：
	def dex(self):
		p = re.compile('<li id="n-national-dex"><a href="(.+?)">')
		url = p.findall(self.maintext)[0]
		return self.combine(url)
	# 获得招式列表网址：
	def move(self):
		p = re.compile('<li id="n-move-list"><a href="(.+?)">')
		url = p.findall(self.maintext)[0]
		return self.combine(url)
	# 获得道具列表网址：
	def item(self):
		p = re.compile('<li id="n-item-list"><a href="(.+?)">')
		url = p.findall(self.maintext)[0]
		return self.combine(url)
	# 获得特性列表网址：
	def ability(self):
		p = re.compile('<a href="(.+?)" title="特性列表">')
		url = p.findall(self.maintext)[0]
		return self.combine(url)

def test():
	p = pokeparser()
	print(p.dex())
	print(p.move())
	print(p.item())
	print(p.ability())

if __name__ == "__main__":
	test()